
package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.cashregister.bean.addressBean;
import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.GetJsonDataUtil;
import com.ukao.cashregister.view.AddressView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class AddAddressPesenter extends BasePresenter<AddressView> {

    public AddAddressPesenter(AddressView view) {
        attachView(view);
    }

    private ArrayList<addressBean> options1Items = new ArrayList<addressBean>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();


    /**
     *添加用户地址
     */
    public void placeOrderData( HashMap mHashMap   ) {
        mvpView.showLoading();
        addSubscription(apiStores.adduserAddress(mHashMap),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode()==200){

                            mvpView.addressDataSucceed();

                        }else{

                            mvpView.addressDataFailure(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    public void userAddressEdit( HashMap mHashMap   ) {
        mvpView.showLoading();
        addSubscription(apiStores.userAddressEdit(mHashMap),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){

                            mvpView.editAddressSucceed(model.getMsg());

                        }else{

                            mvpView.addressDataFailure(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




    public  void  getAddressProvinceData(Context _mActivity ) {
        String JsonData = new GetJsonDataUtil().getJson(_mActivity, "province.json");
        Gson mGson = new Gson();
        ArrayList<addressBean> provinceCityLists = mGson.fromJson(JsonData,
                new TypeToken<List<addressBean>>() {
                }.getType());

        options1Items = (ArrayList<addressBean>) provinceCityLists;

        for (int i = 0; i < provinceCityLists.size(); i++) {    //遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c = 0; c < provinceCityLists.get(i).getChildren().size(); c++) {  //遍历该省份的所有城市
                String CityName = provinceCityLists.get(i).getChildren().get(c).getLabel();
                CityList.add(CityName);//添加城市

                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (provinceCityLists.get(i).getChildren().get(c).getChildren() == null
                        || provinceCityLists.get(i).getChildren().get(c).getChildren().size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < provinceCityLists.get(i).getChildren().get(c).getChildren().size(); d++) {//该城市对应地区所有数据
                        String AreaName = provinceCityLists.get(i).getChildren().get(c).getChildren().get(d).getLabel();

                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            options3Items.add(Province_AreaList);
        }
        mvpView.cityDataSucceed(provinceCityLists,options1Items,options2Items,options3Items);
    }


}
